﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DMS.Business.WebControls
{
    /// <summary> 
    /// Summary description for AutoLengthTextBox. 
    /// </summary> 
    [DefaultProperty("Text"),
    ToolboxData("<{0}:AutoTextArea runat =server></{0}:AutoTextArea>")]
    public class AutoTextArea : TextBox
    {
        [DefaultValue(200)]
        public int MaxHeight
        {
            get
            {
                object obj = ViewState["MaxHeight"];
                return obj == null ? 200 : (int)obj;
            }
            set
            {
                ViewState["MaxHeight"] = value;
            }
        }

        [DefaultValue(60)]
        public int MinHeight
        {
            get
            {
                object obj = ViewState["MinHeight"];
                return obj == null ? 60 : (int)obj;
            }
            set
            {
                ViewState["MinHeight"] = value;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Attributes["minHeight"] = this.MinHeight.ToString();
            if (this.Height == Unit.Empty)
            {
                this.Height = this.MinHeight;
            }
            else
            {
                this.Height = (int)Math.Max(this.MinHeight, this.Height.Value);
            }
            base.OnPreRender(e);
        }

        /**/
        /// <summary> 
        /// Render this control to the output parameter specified. 
        /// </summary> 
        /// <param name="output"> The HTML writer to write out to </param> 
        protected override void Render(HtmlTextWriter output)
        {
            string strCode;
            if (this.MaxHeight <= 0)
            {
                strCode = "this.style.height=Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)";
            }
            else
            {
                strCode = "this.style.height=(this.scrollHeight>200)?200:Math.max(this.minHeight,this.scrollHeight)+(this.offsetHeight-this.clientHeight)";
            }
            base.Attributes["onpropertychange"] = strCode;
            // base.Attributes["onfocus"] = "this.height=this.height"; 
            if (base.Rows == 0)
            {
                base.Rows = 1;
            }
            base.TextMode = TextBoxMode.MultiLine;
            base.Render(output);
        }

    }
}
